<template>
  <div class="AccountBalance">
    <div class="header">
      <h2>科目余额表</h2>
      <div>
        <div>
          <span>查询时间：</span>
          <el-date-picker
            v-model="searchData.settleDate"
            type="month"
            placeholder="选择日期"
            value-format="yyyyMM"
          >
          </el-date-picker>
        </div>
        <div>
          <el-button type="primary" @click="search">查询</el-button>
          <el-button type="primary" @click="add">新增科目</el-button>
        </div>
      </div>
      <hr />
    </div>
    <div class="content">
      <editable-table
        :tableData="tableData && tableData.list && tableData.list.list"
        :columns="tableData && tableData.fields"
        :loading="loading"
        @edit-save="handleSaveEdit"
      ></editable-table>
    </div>
    <div class="footer">
      <pagination
        :current-page="searchData.pageDate.pageNum"
        :page-size="searchData.pageDate.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div v-if="maskFlag" class="mask">
      <div class="table">
        <div class="flex">
          <div class="title">新增科目数据</div>
          <i class="el-icon-close" @click="close"></i>
        </div>
        <div class="custom">
          <el-form
            label-position="left"
            label-width="150px"
            ref="ruleForm"
            :rules="rules"
            :model="formData"
          >
            <el-form-item label="日期" prop="month">
              <el-date-picker
                v-model="formData.month"
                type="month"
                placeholder="选择日期"
                value-format="yyyyMM"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="一级科目名称" prop="firstSubjectName">
              <el-input v-model.number="formData.firstSubjectName"></el-input>
            </el-form-item>
            <el-form-item label="二级科目名称" prop="secondSubjectName">
              <el-input v-model.number="formData.secondSubjectName"></el-input>
            </el-form-item>
            <el-form-item label="期初余额" prop="openingBalance">
              <el-input v-model.number="formData.openingBalance"></el-input>
            </el-form-item>
            <el-form-item label="本月借方金额" prop="monthDebitAmount">
              <el-input v-model.number="formData.monthDebitAmount"></el-input>
            </el-form-item>
            <el-form-item label="本月贷方金额" prop="monthCreditAmount">
              <el-input v-model.number="formData.monthCreditAmount"></el-input>
            </el-form-item>
            <el-form-item label="期末余额" prop="endingBalance">
              <el-input v-model.number="formData.endingBalance"></el-input>
            </el-form-item>
          </el-form>
          <div class="btn">
            <el-button type="primary" round @click="save">保存</el-button>
            <el-button round @click="close">取消</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getAccountBalanceList,
  addAccountBalanceList,
  editAccountBalanceList,
} from "@/api/statisticalCenter";

export default {
  name: "AccountBalance",
  data() {
    return {
      searchData: {
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
        settleDate: this.getCurrentDate(),
      },
      total: 0,
      tableData: [],
      loading: false,
      maskFlag: false,
      formData: {
        firstSubjectName: "",
        secondSubjectName: "",
        openingBalance: "",
        monthDebitAmount: "",
        monthCreditAmount: "",
        endingBalance: "",
        month: "",
      },
      rules: {
        firstSubjectName: [{ required: true, message: "必填项" }],
        secondSubjectName: [{ required: true, message: "必填项" }],
        openingBalance: [{ required: true, message: "必填项" }],
        monthDebitAmount: [{ required: true, message: "必填项" }],
        monthCreditAmount: [{ required: true, message: "必填项" }],
        endingBalance: [{ required: true, message: "必填项" }],
        month: [{ required: true, message: "必填项" }],
      },
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    getCurrentDate() {
      const date = new Date();
      const year = date.getFullYear();
      let month = date.getMonth() + 1;
      month = month < 10 ? "0" + month : month;
      return `${year}${month}`;
    },
    search() {
      if (this.searchData.settleDate) {
        this.getTableData();
      } else {
        this.$message.warning("请选择查询时间");
      }
    },
    save() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          addAccountBalanceList(this.formData).then((res) => {
            if (res.code == 200) {
              this.$message.success("添加成功");
              this.close();
              this.getTableData();
            }
          });
        } else {
          return false;
        }
      });
    },
    add() {
      this.editFlag = false;
      this.maskFlag = true;
    },
    close() {
      this.maskFlag = false;
      this.formData = {};
    },
    getTableData() {
      this.loading = true;
      getAccountBalanceList(this.searchData).then((res) => {
        this.tableData = res.data;
        this.total = res.data.list.total;
        this.loading = false;
      });
    },
    handleSaveEdit(row) {
      editAccountBalanceList(row).then((res) => {
        if (res.code == 200) {
          this.$message.success("修改成功");
          this.getTableData();
        }
      });
    },
    handleSizeChange(size) {
      this.searchData.pageDate.pageSize = size;
      this.getTableData();
    },
    handleCurrentChange(current) {
      this.searchData.pageDate.pageNum = current;
      this.getTableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.AccountBalance {
  padding: 20px;
}
.header {
  h2 {
    text-align: center;
    color: skyblue;
  }
  & > div {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    & > div {
      margin-right: 20px;
    }
  }
  hr {
    margin: 16px 0;
    color: skyblue;
  }
}
.footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  background: rgba($color: #000000, $alpha: 0.2);
  .flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
  }
  .title {
    padding-left: 12px;
    border-left: 6px solid skyblue;
    font-size: 24px;
    font-weight: bold;
  }
  i {
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
  }
  .table {
    margin: 80px auto;
    padding: 30px;
    width: 30%;
    border-radius: 24px;
    background-color: #fff;
    .custom {
      margin-bottom: 20px;
      padding: 0 20px;
      .btn {
        display: flex;
        justify-content: space-around;
        .el-button {
          padding: 12px 36px;
          font-size: 16px;
        }
      }
    }
  }
  &::v-deep .t-table {
    .t-table__header {
      tr th {
        font-size: 16px;
        font-weight: bolder;
      }
    }
    .t-table__body {
      tr td {
        font-size: 16px;
        font-weight: bolder;
        color: #666;
      }
    }
  }
}
</style>
